package com.ccbooks.view;

import java.util.ArrayList;
import java.util.List;

import com.ccbooks.adapter.ShelfFloorAdapter;
import com.ccbooks.bo.BooksBo;
import com.ccbooks.bo.FileBo;
import com.ccbooks.dao.BooksDao;
import com.ccbooks.util.BookUtil;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.AbsListView.OnScrollListener;

public class BookShelfView extends ListActivity implements OnScrollListener {

	// ��ÿؼ�
	private Button btnBookStore;
	private Button btnBookshelf;
	private Button btnBooklist;
	private Button btnEdit;
	private Button btnReflesh;

	private ShelfFloorAdapter sfa;

	public List<ImageView> ivDels; // ɾ��ť�¿ؼ������ã�
	public boolean isEdit;
	public int orientation;
	/**对话框*/
	private Dialog pd;
	// public List<Book> books;
	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		getListView().setScrollbarFadingEnabled(true);
		findview();
		addListener();
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		doing();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("zkli", "shell finishing====");
		BooksDao.getDao(this).close();
	}
	

/*	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { // ����
			// doSomrthing
			showDialog(1);
		} else {
			// ����ʱdosomething
			showDialog(2);
		}
	}*/

	private void findview() {
		btnBookStore = (Button) findViewById(R.id.btnBookStore);
		btnBookshelf = (Button) findViewById(R.id.btnBookshelf);
		btnBooklist = (Button) findViewById(R.id.btnBooklist);
		btnEdit = (Button) findViewById(R.id.btnEdit);
		btnReflesh = (Button) findViewById(R.id.btnReflesh);
	}
	
	private void addListener(){
		btnBookshelf.setBackgroundDrawable(getResources().getDrawable(R.drawable.shelfonclick));
		btnBooklist.setOnClickListener(btnBookshelfOnclick);
		btnReflesh.setOnClickListener(btnRefleshOnclick);
		btnEdit.setOnClickListener(btnEditOnclick);
	}

	private void doing() {
		
		
		BooksBo bunit = new BooksBo(this, false);
		bunit.firstShellBooks();
		ivDels = new ArrayList<ImageView>();
		sfa = new ShelfFloorAdapter(this, BooksBo.books);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { // ����
			// doSomrthing
			orientation=1;
		} else {
			// ����ʱdosomething
			orientation=2;
		}
		showview(orientation);
		//new Thread(bunit).start();
	}

	/**
	 * @param orientation ������������Ϊ2������Ϊ����
	 * */
	private void showview(int orientation) {	
		sfa.orientation=orientation;
		setListAdapter(sfa);
		getListView().setOnScrollListener(this);		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (BookUtil.bookSize(BooksBo.books) < BookUtil.getCount(this)) {
			// sfa.count += visibleItemCount; // or any other amount
			sfa.notifyDataSetChanged();

		}

	}
	
	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	// ���ּ���
	//各种事件监听
	//按跳转书库按钮
	private OnClickListener btnBookshelfOnclick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(BookShelfView.this, BookListView.class);
			startActivity(intent);
			finish();
		}

	};

	private OnClickListener btnRefleshOnclick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			pd = new Dialog(BookShelfView.this);
			pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
			pd.setContentView(R.layout.dia_loading);
			FileBo fileUtil = new FileBo(BookShelfView.this, "/sdcard");
			//fileUtil.refreshFileList("/sdcard");
			new Thread(fileUtil).start();		
			pd.show();
		}
		
	};
	
	
	private OnClickListener btnEditOnclick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			isEdit = !isEdit;
			for (ImageView iv : ivDels) {
				if (isEdit) {
					iv.setVisibility(View.VISIBLE);
				} else {
					iv.setVisibility(View.GONE);
				}
			}
			if(isEdit){
				btnEdit.setText(R.string.Complete);
				btnEdit.setBackgroundDrawable(BookShelfView.this.getResources()
						.getDrawable(R.drawable.onclickbutton));
			}else{
				btnEdit.setText(R.string.Edit);
				btnEdit.setBackgroundDrawable(BookShelfView.this.getResources()
						.getDrawable(R.drawable.outclickbutton));
			}
		}

	};
	//刷新Handler
	public Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			doing();
			if(pd != null){
				pd.cancel();
			}
			super.handleMessage(msg);
		}
		
	};
}